package com.pwx.student.service.bo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pwx.student.common.Const;
import com.pwx.student.dao.RcAdminMapper;
import com.pwx.student.dao.RcStudentMapper;
import com.pwx.student.dao.RcTeacherMapper;
import com.pwx.student.entity.RcAdmin;
import com.pwx.student.entity.RcStudent;
import com.pwx.student.entity.RcTeacher;
import com.pwx.student.utils.MD5Utils;
import com.pwx.student.vo.input.LoginInputVo;
import com.pwx.student.vo.output.LoginOutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: LoginServiceBo
 * Author: saber
 * Date:2022/10/26 16:38
 */
@Service
public class LoginServiceBo {

    @Autowired
    private RcStudentMapper studentMapper;
    @Autowired
    private RcTeacherMapper teacherMapper;
    @Autowired
    private RcAdminMapper adminMapper;

    public LoginOutputVo doLogin(LoginInputVo inputVo){
        LoginOutputVo outputVo = null;
        if (inputVo.getType().equals(Const.UserType.STUDENT)){
            RcStudent student = studentMapper.selectOne(new QueryWrapper<RcStudent>()
                    .eq("student_number", inputVo.getUsername()));
            if (student!=null&&student.getStudentPassword().equals(inputVo.getPassword())){
                outputVo = new LoginOutputVo (student.getStudentId(),
                    student.getStudentName(),
                    student.getStudentPassword(),inputVo.getType());
                return outputVo;
            }
        }else if (inputVo.getType().equals(Const.UserType.TEACHER)){
            RcTeacher teacher = teacherMapper.selectOne(new QueryWrapper<RcTeacher>()
                    .eq("teacher_number", inputVo.getUsername()));
            if (teacher!=null&&teacher.getTeacherPassword().equals(inputVo.getPassword())){
                outputVo = new LoginOutputVo (teacher.getTeacherId(),
                        teacher.getTeacherName(),
                        teacher.getTeacherPassword(),inputVo.getType());
                return outputVo;
            }
        }else if (inputVo.getType().equals(Const.UserType.ADMIN)){
            RcAdmin admin = adminMapper.selectOne(new QueryWrapper<RcAdmin>()
                    .eq("admin_username", inputVo.getUsername()));
            if (admin!=null&&admin.getAdminPassword().equals(inputVo.getPassword())){
                outputVo = new LoginOutputVo (admin.getAdminId(),
                        admin.getAdminUsername(),
                        admin.getAdminPassword(),inputVo.getType());
                return outputVo;
            }
        }
        return null;
    }

    private String LockPassWord(String password){
        return MD5Utils.md5(password);
    }

} 
